using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using umbraco.cms.businesslogic.member;
using umbraco.cms.businesslogic.property;
using Commerce.Common;

namespace CommerceForUmbraco.profile
{
    public class UmbracoProfile
    {
        private Member m = null;

        public UmbracoProfile()
        {
            SetProfile(null);
        }

        public UmbracoProfile(string username)
        {
            SetProfile(username);
        }

        #region Member Properties

        private string _UserName;

        public string UserName
        {
            get
            {
                _UserName = m.Text;
                return _UserName;
            }
            set { _UserName = value; }
        }

        private string _Email;

        public string Email
        {
            get { return _Email; }
            set 
            { 
                _Email = value;
                m.Email = _Email;
            }
        }

        #endregion

        #region Billing Address

        private string _BillingFirstName;

        public string BillingFirstName
        {
            get
            { return _BillingFirstName; }
            set
            {
                _BillingFirstName = value;
                m.getProperty("billingFirstName").Value = _BillingFirstName;
            }
        }

        private string _BillingLastName;

        public string BillingLastName
        {
            get { return _BillingLastName; }
            set
            {
                _BillingLastName = value;
                m.getProperty("billingLastName").Value = _BillingLastName;
            }
        }

        private string _BillingAddress1;
        public string BillingAddress1
        {
            get { return _BillingAddress1; }
            set 
            { 
                _BillingAddress1 = value;
                m.getProperty("billingAddress1").Value = _BillingAddress1;
            }
        }

        private string _BillingAddress2;
        public string BillingAddress2
        {
            get { return _BillingAddress2; }
            set
            {
                _BillingAddress2 = value;
                m.getProperty("billingAddress2").Value = _BillingAddress2;
            }
        }

        private string _BillingPhone;
        public string BillingPhone
        {
            get { return _BillingPhone; }
            set
            {
                _BillingPhone = value;
                m.getProperty("billingPhone").Value = _BillingPhone;
            }
        }

        private string _BillingEmail;
        public string BillingEmail
        {
            get { return _BillingEmail; }
            set
            {
                _BillingEmail = value;
                m.getProperty("billingEmail").Value = _BillingEmail;
            }
        }

        private string _BillingCity;
        public string BillingCity
        {
            get { return _BillingCity; }
            set
            {
                _BillingCity = value;
                m.getProperty("billingCity").Value = _BillingCity;
            }
        }

        private string _BillingStateOrRegion;
        public string BillingStateOrRegion
        {
            get { return _BillingStateOrRegion; }
            set
            {
                _BillingStateOrRegion = value;
                m.getProperty("billingStateOrRegion").Value = _BillingStateOrRegion;
            }
        }

        private string _BillingZip;
        public string BillingZip
        {
            get { return _BillingZip; }
            set
            {
                _BillingZip = value;
                m.getProperty("billingZip").Value = _BillingZip;
            }
        }

        private string _BillingCountry;
        public string BillingCountry
        {
            get { return _BillingCountry; }
            set
            {
                _BillingCountry = value;
                m.getProperty("billingCountry").Value = _BillingCountry;
            }
        }
        #endregion

        #region Shipping Address

        private string _ShippingFirstName;

        public string ShippingFirstName
        {
            get
            { return _ShippingFirstName; }
            set
            {
                _ShippingFirstName = value;
                m.getProperty("shippingFirstName").Value = _ShippingFirstName;
            }
        }

        private string _ShippingLastName;

        public string ShippingLastName
        {
            get { return _ShippingLastName; }
            set
            {
                _ShippingLastName = value;
                m.getProperty("shippingLastName").Value = _ShippingLastName;
            }
        }

        private string _ShippingAddress1;
        public string ShippingAddress1
        {
            get { return _ShippingAddress1; }
            set 
            { 
                _ShippingAddress1 = value;
                m.getProperty("shippingAddress1").Value = _ShippingAddress1;
            }
        }

        private string _ShippingAddress2;
        public string ShippingAddress2
        {
            get { return _ShippingAddress2; }
            set
            {
                _ShippingAddress2 = value;
                m.getProperty("shippingAddress2").Value = _ShippingAddress2;
            }
        }

        private string _ShippingPhone;
        public string ShippingPhone
        {
            get { return _ShippingPhone; }
            set
            {
                _ShippingPhone = value;
                m.getProperty("shippingPhone").Value = _ShippingPhone;
            }
        }

        private string _ShippingEmail;
        public string ShippingEmail
        {
            get { return _ShippingEmail; }
            set
            {
                _ShippingEmail = value;
                m.getProperty("shippingEmail").Value = _ShippingEmail;
            }
        }

        private string _ShippingCity;
        public string ShippingCity
        {
            get { return _ShippingCity; }
            set
            {
                _ShippingCity = value;
                m.getProperty("shippingCity").Value = _ShippingCity;
            }
        }

        private string _ShippingStateOrRegion;
        public string ShippingStateOrRegion
        {
            get { return _ShippingStateOrRegion; }
            set
            {
                _ShippingStateOrRegion = value;
                m.getProperty("shippingStateOrRegion").Value = _ShippingStateOrRegion;
            }
        }

        private string _ShippingZip;
        public string ShippingZip
        {
            get { return _ShippingZip; }
            set
            {
                _ShippingZip = value;
                m.getProperty("shippingZip").Value = _ShippingZip;
            }
        }

        private string _ShippingCountry;
        public string ShippingCountry
        {
            get { return _ShippingCountry; }
            set
            {
                _ShippingCountry = value;
                m.getProperty("shippingCountry").Value = _ShippingCountry;
            }
        }
        #endregion

        #region Commerce Properties

        private string _CurrentOrderTax;

        public string CurrentOrderTax
        {
            get { return _CurrentOrderTax; }
            set { 
                _CurrentOrderTax = value;
                m.getProperty("currentOrderTax").Value = _CurrentOrderTax;
            }
        }

        private string _PayPalPayerID;

        public string PayPalPayerID
        {
            get { return _PayPalPayerID; }
            set { 
                _PayPalPayerID = value;
                m.getProperty("PayPalPayerID").Value = _PayPalPayerID;
                }
        }

        private string _PayPalToken;

        public string PayPalToken
        {
            get { return _PayPalToken; }
            set { 
                _PayPalToken = value;
                m.getProperty("PayPalToken").Value = _PayPalToken;    
            }
        }

        private string _CurrentOrderShipping;

        public string CurrentOrderShipping
        {
            get { return _CurrentOrderShipping; }
            set 
            { 
                _CurrentOrderShipping = value;
                m.getProperty("CurrentOrderShipping").Value = _CurrentOrderShipping;
            }
        }
	

        private string _CurrentOrderShippingMethod;

        public string CurrentOrderShippingMethod
        {
            get { return _CurrentOrderShippingMethod; }
            set 
            { 
                _CurrentOrderShippingMethod = value;
                m.getProperty("CurrentOrderShippingMethod").Value = _CurrentOrderShippingMethod;
            }
        }
	

        private string _IpAddress;

        public string IpAddress
        {
            get { return _IpAddress; }
            set { 
                _IpAddress = value;
                m.getProperty("ipAddress").Value = _IpAddress;
            }
        }

        private bool _OptIn;

        public bool OptIn
        {
            get { return _OptIn; }
            set { 
                _OptIn = value;
                m.getProperty("optIn").Value = _OptIn;
            }
        }

        #endregion

        #region Methods
        
        public Address GetCommerceBillingAddress()
        {
            Address commerceAddress = new Address();

            commerceAddress.FirstName = _BillingFirstName;
            commerceAddress.LastName = _BillingLastName;
            commerceAddress.Address1 = _BillingAddress1;
            commerceAddress.Address2 = _BillingAddress2;
            commerceAddress.City = _BillingCity;
            commerceAddress.Zip = _BillingZip;
            commerceAddress.StateOrRegion = _BillingStateOrRegion;
            commerceAddress.Country = _BillingCountry;
            commerceAddress.Email = _BillingEmail;
            commerceAddress.Phone = _BillingPhone;

            return commerceAddress;
        }

        public Address GetCommerceShippingAddress()
        {
            Address commerceAddress = new Address();

            commerceAddress.FirstName = _ShippingFirstName;
            commerceAddress.LastName = _ShippingLastName;
            commerceAddress.Address1 = _ShippingAddress1;
            commerceAddress.Address2 = _ShippingAddress2;
            commerceAddress.City = _ShippingCity;
            commerceAddress.Zip = _ShippingZip;
            commerceAddress.StateOrRegion = _ShippingStateOrRegion;
            commerceAddress.Country = _ShippingCountry;
            commerceAddress.Email = _ShippingEmail;
            commerceAddress.Phone = _ShippingPhone;

            return commerceAddress;
        }

        private void SetProfile(string username)
        {
            decimal defaultTax;

            string memberLogin = "";
            if (username != null)
                memberLogin = username;

            // when no member parsed, show current member
            if (memberLogin == "" && umbraco.cms.businesslogic.member.Member.GetCurrentMember() != null)
                memberLogin = umbraco.cms.businesslogic.member.Member.GetCurrentMember().LoginName;

            if (memberLogin != "")
            {
                try
                {
                    m = Member.GetMemberFromLoginName(memberLogin);
                }
                catch { }

                if (m != null)
                {
                    // set properties
                    _Email = m.Email;
                    _UserName = m.Text;
                    // Billing Address
                    _BillingFirstName = m.getProperty("billingFirstName").Value.ToString();
                    _BillingLastName = m.getProperty("billingLastName").Value.ToString();
                    _BillingAddress1 = m.getProperty("billingAddress1").Value.ToString();
                    _BillingAddress2 = m.getProperty("billingAddress2").Value.ToString();
                    _BillingCity = m.getProperty("billingCity").Value.ToString();
                    _BillingStateOrRegion = m.getProperty("billingStateOrRegion").Value.ToString();
                    _BillingZip = m.getProperty("billingZip").Value.ToString();
                    _BillingCountry = m.getProperty("billingCountry").Value.ToString();
                    _BillingPhone = m.getProperty("billingPhone").Value.ToString();
                    _BillingEmail = m.getProperty("billingEmail").Value.ToString();
                    if (_BillingEmail.Length == 0)
                        _BillingEmail = _Email;
                    // Shipping Address
                    _ShippingFirstName = m.getProperty("shippingFirstName").Value.ToString();
                    _ShippingLastName = m.getProperty("shippingLastName").Value.ToString();
                    _ShippingAddress1 = m.getProperty("shippingAddress1").Value.ToString();
                    _ShippingAddress2 = m.getProperty("shippingAddress2").Value.ToString();
                    _ShippingCity = m.getProperty("shippingCity").Value.ToString();
                    _ShippingStateOrRegion = m.getProperty("shippingStateOrRegion").Value.ToString();
                    _ShippingZip = m.getProperty("shippingZip").Value.ToString();
                    _ShippingCountry = m.getProperty("shippingCountry").Value.ToString();
                    _ShippingPhone = m.getProperty("shippingPhone").Value.ToString();
                    _ShippingEmail = m.getProperty("shippingEmail").Value.ToString();
                    if (_ShippingEmail.Length == 0)
                        _ShippingEmail = _Email;
                    // Commerce Data
                    if (decimal.TryParse((m.getProperty("currentOrderTax").Value.ToString()), out defaultTax))
                        _CurrentOrderTax = defaultTax.ToString();
                    else
                        _CurrentOrderTax = "0";
                    _PayPalPayerID = m.getProperty("PayPalPayerID").Value.ToString();
                    _PayPalToken = m.getProperty("PayPalToken").Value.ToString();
                    _CurrentOrderShipping = m.getProperty("CurrentOrderShipping").Value.ToString();
                    _CurrentOrderShippingMethod = m.getProperty("CurrentOrderShippingMethod").Value.ToString();
                    _IpAddress = m.getProperty("ipAddress").Value.ToString();
                    if (m.getProperty("optIn").Value.ToString() == "1")
                        _OptIn = true;
                    else
                        _OptIn = false;
                }

            }
        }

        #endregion
    }
}
